package day3.homework;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public
class Dome {
    static List<User1> userList = new ArrayList<>();
    static Scanner sc = new Scanner(System.in);
    static {//静态代码块，在类加载时执行
        //刚刚打开系统，就需要读取文件中的数据 做反序列化
        input();
    }
    public static
    void main(String[] args) {
        menu();
    }
    public static
    void menu() {
        while (true) {
            System.out.println("---------------------------------");
            System.out.println("用户管理系统");
            System.out.println("1.注册功能");
            System.out.println("2.登录功能");
            System.out.println("3.退出系统");
            System.out.println("请输入操作");
            System.out.println("---------------------------------");
            int choice = sc.nextInt();
            sc.nextLine();
            switch (choice) {
                case 1: register();break;
                case 2: login(); break;
                case 3: return;
            }
        }
    }
    public static
    void menu1(User1 currentUser) {
        while (true) {
            System.out.println("---------------------------------");
            System.out.println("二级菜单");
            System.out.println("1.展示用户信息");
            System.out.println("2.删除用户信息");
            System.out.println("3.返回一级菜单");
            System.out.println("请输入操作");
            System.out.println("---------------------------------");
            int choice = sc.nextInt();
            sc.nextLine();
            switch (choice) {
                case 1: show(currentUser);break;
                case 2: delete(currentUser); break;
                case 3: return;
            }
        }
    }
    public static void register() {
        Random r = new Random();
        System.out.println("在以下号码中选择一个进行注册：");
        for (int i = 0; i < 9; i++) {
            long x = r.nextInt(99999999) + 15000000000l;
            System.out.print(x + "\t");
        }
        System.out.println();
        long phone;
        while (true) {
            System.out.println("输入注册的电话号码：");
            phone = sc.nextLong();
            if (String.valueOf(phone).length() != 11) {
                System.out.println("手机号错误，请重新输入");
            } else {
                break;
            }
        }
        System.out.println("输入注册的密码：");
        long password = sc.nextLong();
        System.out.println("输入注册的姓名：");
        String name = sc.next();
        userList.add(new User1(phone, password, name));
        System.out.println("注册成功");
        output();
    }
    public static void login() {
        System.out.println("输入登录的电话号码：");
        long phone = sc.nextLong();
        System.out.println("输入登录的密码：");
        long password = sc.nextLong();

        User1 currentUser = null;
        for (User1 user : userList) {
            if (user.getPhone() == phone && user.getPassword() == password) {
                currentUser = user;
                break;
            }
        }

        if (currentUser != null) {
            System.out.println("登录成功！");

        } else {
            System.out.println("登录失败，手机号或密码错误！");
        }
    }
    public static void show(User1 currentUser){
        System.out.println("编号\t手机号\t密码\t姓名");
        for(int i=0;i<userList.size();i++){
            User1 u=userList.get(i);//获取集合中的每一个元素
            System.out.println((i+1)+"\t"+u.getPhone()+"\t"+u.getPassword()+"\t"+u.getName());
        }
    }
    public static void delete(User1 currentUser){
        show(currentUser);
        System.out.println("输入需要删除的编号");
        int id= sc.nextInt();
        if(id>userList.size()||id<=0){
            System.out.println("编号不存在！");
            return;
        }
        User1 user=userList.get(id-1);
        if(user.getPhone()==currentUser.getPhone()){
            System.out.println("本机无法删除自己的账号！");
        }else{
            userList.remove(id-1);
            System.out.println("删除成功！");
        }
        output();
    }
    public static void output(){
        try (
                FileOutputStream fos=new FileOutputStream("D:\\IO\\user1.txt");
                ObjectOutputStream oos=new ObjectOutputStream(fos);
        ){
            oos.writeObject(userList);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void input(){
        try (
                FileInputStream fis=new FileInputStream("D:\\IO\\user1.txt");
                ObjectInputStream ois=new ObjectInputStream(fis);
        ){
            userList= (List<User1>) ois.readObject();
            System.out.println("集合读取成功");
        }catch (FileNotFoundException e){
            System.out.println("欢迎第一使用，系统自动创建文件");
        }catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }
    }
}

class User1 implements Serializable{
    private long phone;
    private long password;
    private String name;



    public User1(long phone,long password,String name){
        this.phone=phone;
        this.password=password;
        this.name=name;
    }

    public
    long getPhone() {
        return phone;
    }

    public
    void setPhone(long phone) {
        this.phone = phone;
    }

    public
    long getPassword() {
        return password;
    }

    public
    void setPassword(long password) {
        this.password = password;
    }

    public
    String getName() {
        return name;
    }

    public
    void setName(String name) {
        this.name = name;
    }
}